package com.bp.hearthealth.pulse.lnapps.ads.topon.rewarded_ad

import android.annotation.SuppressLint
import com.anythink.rewardvideo.api.ATRewardVideoAd
import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.ads.admob.rewarded_ad.AdMobRewardedAdManager
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsSwitch
import com.bp.hearthealth.pulse.lnapps.ads.topon.ATAdSourceStatusListenerImpl
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.listener.ad_source.TopOnAdSourceListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.rewarded_ad.manual_load.ATRewardVideoExListenerImpl
import com.bp.hearthealth.pulse.lnapps.ads.topon.rewarded_ad.manual_load.TopOnRewardVideoAdListener
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants


/**
 * TopOnRewardVideoAdManager.initManualLoadRewardVideoAd()
 *         TopOnRewardVideoAdManager.setManualLoadListener(object : TopOnRewardVideoAdListener{
 *             override fun onRewardedVideoAdLoaded() {
 *                 if (TopOnRewardVideoAdManager.isAdReady()) {
 *                     TopOnRewardVideoAdManager.mRewardVideoAd!!.show(this@BpMainActivity)
 *                 }
 *             }
 *         })
 *         TopOnRewardVideoAdManager.manualLoadAd()
 */
object TopOnRewardVideoAdManager {

    @SuppressLint("StaticFieldLeak")
    var mRewardVideoAd: ATRewardVideoAd? = null
    private var manualListener: ATRewardVideoExListenerImpl? = null
    private var sourceStatusListenerImpl: ATAdSourceStatusListenerImpl? = null
    private var topOnRewardVideoAdListener: TopOnRewardVideoAdListener? = null

    fun isAdReady(): Boolean {
        if (!AdsSwitch.getBoolean(AdsSwitch.adRVControl)) {
            topOnRewardVideoAdListener?.onFireBaseAdIsClosed()
            return false
        }
        if (mRewardVideoAd != null) {
            LogUtil.iTopOn("TopOn 【激励视频广告】 mRewardVideoAd.isAdReady ${mRewardVideoAd!!.isAdReady}")
            return mRewardVideoAd!!.isAdReady
        }
        LogUtil.iTopOn("TopOn 【激励视频广告】 isAdReady(): mRewardVideoAd == null")
        return false
    }

    fun setSourceListener(topOnAdSourceListener: TopOnAdSourceListener?) {
        if (sourceStatusListenerImpl ==null) {
            sourceStatusListenerImpl = ATAdSourceStatusListenerImpl()
        }
        sourceStatusListenerImpl?.setSourceStatusListener(topOnAdSourceListener)
    }

    fun setManualLoadListener(topOnRewardVideoAdListener: TopOnRewardVideoAdListener?) {
        this.topOnRewardVideoAdListener = topOnRewardVideoAdListener
        if (manualListener == null) {
            manualListener = ATRewardVideoExListenerImpl()
        }
        manualListener!!.setManualLoadListener(topOnRewardVideoAdListener)
    }

    fun initManualLoadRewardVideoAd() {
        if (manualListener == null) {
            manualListener = ATRewardVideoExListenerImpl()
        }
        if (mRewardVideoAd == null) {
            mRewardVideoAd = ATRewardVideoAd(BpApplication.context, Constants.TOP_ON_REWARDED_ID)
        }
        mRewardVideoAd!!.setAdListener(manualListener)
//        mRewardVideoAd!!.setAdSourceStatusListener(sourceStatusListenerImpl)
    }


    fun manualLoadAd() {
        if (!AdsSwitch.getBoolean(AdsSwitch.adRVControl)) {
            topOnRewardVideoAdListener?.onFireBaseAdIsClosed()
            return
        }
        if (mRewardVideoAd != null) {
            mRewardVideoAd!!.load()
        } else {
            LogUtil.iTopOn("TopOn 【激励视频广告】 mRewardVideoAd == null！！！！！！！！")
        }
    }

    fun releaseManualLoadRewardVideoAd() {
        if (mRewardVideoAd != null) {
            setSourceListener(null)
            setManualLoadListener(null)
            mRewardVideoAd!!.setAdListener(null)
            mRewardVideoAd!!.setAdDownloadListener(null)
            mRewardVideoAd!!.setAdSourceStatusListener(null)
            mRewardVideoAd = null
        }
    }
}

